import SwiftUI
import shared

struct ContentView: View {
    // 创建ViewModel实例
    private let videoListViewModel = VideoListViewModel(videoScanner: LocalVideoScanner())
    
    // 监听是否显示播放器
    @State private var showPlayer = false
    
    var body: some View {
        ZStack {
            // 主内容区域 - 底部TabView
            if !showPlayer {
                TabView {
                    // 视频标签页
                    VideoTabView(viewModel: videoListViewModel, onShowPlayer: { showPlayer = true })
                        .tabItem {
                            Label("视频", systemImage: "list.bullet")
                        }
                    
                    // 音频标签页
                    AudioTabView()
                        .tabItem {
                            Label("音频", systemImage: "list.bullet")
                        }
                    
                    // 设置标签页
                    SettingsTabView(viewModel: videoListViewModel)
                        .tabItem {
                            Label("设置", systemImage: "gear")
                        }
                }
                .accentColor(.blue) // 设置选中标签的颜色
            }
            
            // 视频播放器全屏视图
            if showPlayer, let selectedVideo = videoListViewModel.selectedVideo as? VideoItem {
                VideoPlayerScreen(
                    videoItem: selectedVideo,
                    viewModel: videoListViewModel,
                    onClose: { 
                        showPlayer = false
                        videoListViewModel.closePlayer()
                    }
                )
            }
        }
    }
}

// 视频标签页视图
struct VideoTabView: View {
    private let viewModel: VideoListViewModel
    private let onShowPlayer: () -> Void
    
    init(viewModel: VideoListViewModel, onShowPlayer: @escaping () -> Void) {
        self.viewModel = viewModel
        self.onShowPlayer = onShowPlayer
    }
    
    var body: some View {
        VStack {
            // 集成共享的VideoFeed组件
            VideoFeed(
                videos: viewModel.videoListState as! VideoListState,
                onVideoClick: { videoItem in
                    viewModel.onVideoClick(video: videoItem)
                    onShowPlayer()
                },
                onLoadMore: {
                    viewModel.loadMoreVideos(limit: 10)
                }
            )
        }
        .navigationTitle("本地视频")
        .onAppear {
            // 加载视频数据
            viewModel.loadLocalVideos(limit: 10)
        }
    }
}



// 视频播放器屏幕
struct VideoPlayerScreen: View {
    private let videoItem: VideoItem
    private let viewModel: VideoListViewModel
    private let onClose: () -> Void
    
    init(videoItem: VideoItem, viewModel: VideoListViewModel, onClose: @escaping () -> Void) {
        self.videoItem = videoItem
        self.viewModel = viewModel
        self.onClose = onClose
    }
    
    var body: some View {
        ZStack {
            Color.black.edgesIgnoringSafeArea(.all)
            
            VStack {
                Spacer()
                Text("视频播放器: \(videoItem.title)")
                    .foregroundColor(.white)
                    .font(.headline)
                Text("URL: \(videoItem.videoUrl)")
                    .foregroundColor(.white)
                    .font(.caption)
                Spacer()
            }
            
            // 关闭按钮
            VStack {
                HStack {
                    Button(action: onClose) {
                        Image(systemName: "xmark")
                            .foregroundColor(.white)
                            .padding()
                            .background(Color.black.opacity(0.5))
                            .clipShape(Circle())
                    }
                    Spacer()
                }
                Spacer()
            }
            .padding()
            
            // 播放控制按钮
            HStack {
                Spacer()
                
                // 上一个视频按钮
                Button(action: {
                    if let previousVideo = viewModel.getPreviousVideo() {
                        viewModel.selectedVideo = previousVideo
                    }
                }) {
                    Image(systemName: "backward.fill")
                        .foregroundColor(.white)
                        .padding()
                        .background(Color.black.opacity(0.5))
                        .clipShape(Circle())
                }
                
                Spacer()
                
                // 下一个视频按钮
                Button(action: {
                    if let nextVideo = viewModel.getNextVideo() {
                        viewModel.selectedVideo = nextVideo
                    }
                }) {
                    Image(systemName: "forward.fill")
                        .foregroundColor(.white)
                        .padding()
                        .background(Color.black.opacity(0.5))
                        .clipShape(Circle())
                }
                
                Spacer()
            }
        }
        .onAppear {
            // 进入全屏模式
            UIApplication.shared.isStatusBarHidden = true
        }
        .onDisappear {
            // 退出全屏模式
            UIApplication.shared.isStatusBarHidden = false
        }
    }
}

// 音频标签页视图
struct AudioTabView: View {
    var body: some View {
        VStack {
            Text("音频列表")
        }
        .navigationTitle("音频列表")
    }
}

// 设置标签页视图
struct SettingsTabView: View {
    private let viewModel: VideoListViewModel
    
    init(viewModel: VideoListViewModel) {
        self.viewModel = viewModel
    }
    
    var body: some View {
        VStack {
            Text("设置页面")
        }
        .navigationTitle("设置")
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}